

let globalData = {

	imgPre: 'https://nengyuan.chuangxiangdianli.com',

}
var request=function(param){
	var _self = this, 
	url = param.url,
	data = param.data || {}, 
	header = param.header||{};
	
	const token = uni.getStorageSync('token');
	//console.log(token)
	//header['token']=token;
	//var token='9e97c0cc0b5c4d64681330ffe512e891';
	if(token){
		// header={
		//     'token':token,
		// 	'content-type':'application/x-www-form-urlencoded'
		// }
		header.token=token
	}
	if(header['content-type']){
		
	}else{
		header['content-type']='application/x-www-form-urlencoded'
	}
	//拼接完整请求地址 
	var requestUrl = 'https://nengyuan.chuangxiangdianli.com/' + url;
	 //网络请求
	 uni.request({
	        url: requestUrl,
	        method: param.method,
	        header: header || {'content-type' : "application/json"},
	        data: data,
	        success: res => {
				//console.log("网络请求success:" + JSON.stringify(res));
				// console.log(res.data.code)
				
				if(res.data.code==2){
					setTimeout(function(){
					uni.showToast({
					    title: '请登陆',
					    duration: 2000,
						icon:"none",
						success(){
							console.log('请登录')
							setTimeout(function(){
								uni.navigateTo({
									url:"../login/loginWx",
									fail(err) {
										console.log(err)
									}
								})
							},500)

						}
					});	
					},10)

					return
				}
				if(res.data.code==0){
					setTimeout(function(){
					uni.showToast({
					    title: res.data.msg,
					    duration: 2000,
						icon:"none",
						success(){
						}
					});	
					},10)
				
					return
				}
				param.success(res.data)
			},
			fail: (e) => {
				console.log("网络请求fail:" + JSON.stringify(e));
				if(param.fail){
					param.fail(e)
				}
				uni.showModal({
					content:"" + res.errMsg
				});
				
			},
			complete: () => {
				//console.log("网络请求complete");
				if(param.complete){
					param.complete()
				}
				
			}
	
       })
}


// -----------------------------------   出口   ---------------------------------------

export default {

  globalData: globalData,
request
}
